package com.member.shop.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.member.common.enums.OrderDeleteEnum;
import com.member.common.enums.UpDownShelvesEnum;
import com.member.common.utils.DateUtils;
import com.member.shop.domain.SysBizCarousel;
import com.member.shop.domain.SysBizJingang;
import com.member.shop.mapper.SysBizCarouselMapper;
import com.member.shop.service.ISysBizCarouselService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 轮播图管理Service业务层处理
 *
 * @author lixinchen
 * @date 2024-12-26
 */
@Service
public class SysBizCarouselServiceImpl extends ServiceImpl<SysBizCarouselMapper, SysBizCarousel> implements ISysBizCarouselService {
    @Resource
    private SysBizCarouselMapper sysBizCarouselMapper;

    /**
     * 查询轮播图管理
     *
     * @param id 轮播图管理主键
     * @return 轮播图管理
     */
    @Override
    public SysBizCarousel selectSysBizCarouselById(Long id) {
        return sysBizCarouselMapper.selectById(id);
    }

    /**
     * 查询轮播图管理列表
     *
     * @param queryWrapper 轮播图管理
     * @return 轮播图管理
     */
    @Override
    public List<SysBizCarousel> selectSysBizCarouselList(LambdaQueryWrapper<SysBizCarousel> queryWrapper) {
        return sysBizCarouselMapper.selectList(queryWrapper);
    }

    @Override
    public List<SysBizCarousel> selectSysBizCarouselList(SysBizCarousel sysBizCarousel) {
        return sysBizCarouselMapper.selectSysBizCarouselList(sysBizCarousel);
    }

    /**
     * 新增轮播图管理
     *
     * @param sysBizCarousel 轮播图管理
     * @return 结果
     */
    @Override
    public int insertSysBizCarousel(SysBizCarousel sysBizCarousel) {
        sysBizCarousel.setCreateTime(DateUtils.getNowDate());
        return sysBizCarouselMapper.insert(sysBizCarousel);
    }

    /**
     * 修改轮播图管理
     *
     * @param sysBizCarousel 轮播图管理
     * @return 结果
     */
    @Override
    public int updateSysBizCarousel(SysBizCarousel sysBizCarousel) {
        sysBizCarousel.setUpdateTime(DateUtils.getNowDate());
        return sysBizCarouselMapper.updateById(sysBizCarousel);
    }

    /**
     * 批量删除轮播图管理
     *
     * @param ids 需要删除的轮播图管理主键
     * @return 结果
     */
    @Override
    public int deleteSysBizCarouselByIds(Long[] ids) {
        return sysBizCarouselMapper.deleteBatchIds(Arrays.asList(ids));
    }

    /**
     * 删除轮播图管理信息
     *
     * @param id 轮播图管理主键
     * @return 结果
     */
    @Override
    public int deleteSysBizCarouselById(Long id) {
        return sysBizCarouselMapper.deleteById(id);
    }

    //判断该商品是否被轮播图
    @Override
    public boolean hasExist(Long id) {
        LambdaQueryWrapper<SysBizCarousel> sysBizCarouselLambdaQueryWrapper = new LambdaQueryWrapper<>();
        sysBizCarouselLambdaQueryWrapper.eq(SysBizCarousel::getDeleted, OrderDeleteEnum.NOT_DELETE.getKey());
        sysBizCarouselLambdaQueryWrapper.eq(SysBizCarousel::getJumpType, "GOODS_LIST");
        List<SysBizCarousel> sysBizCarousels = sysBizCarouselMapper.selectList(sysBizCarouselLambdaQueryWrapper);
        for (SysBizCarousel sysBizCarousel : sysBizCarousels) {
            String jumpContent = sysBizCarousel.getJumpContent();
            Pattern pattern = Pattern.compile("id=(\\d+)");
            Matcher matcher = pattern.matcher(jumpContent);
            // 输出匹配到的id值
            if (matcher.find()) {
                if (Integer.parseInt(matcher.group(1)) == id) {
                    return true;
                }
            }
        }


        return false;
    }

    // 查询轮播图数量是否超过六个
    @Override
    public boolean selectGroundingNum(SysBizCarousel sysBizCarousel) {
        LambdaQueryWrapper<SysBizCarousel> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SysBizCarousel::getDisplayed, UpDownShelvesEnum.GROUNDING.getKey());
        queryWrapper.eq(SysBizCarousel::getDeleted, OrderDeleteEnum.NOT_DELETE.getKey());
        queryWrapper.eq(SysBizCarousel::getTenantId, sysBizCarousel.getTenantId());
        queryWrapper.eq(SysBizCarousel::getType, sysBizCarousel.getType());
        queryWrapper.ne(sysBizCarousel.getId() != null, SysBizCarousel::getId, sysBizCarousel.getId());
        List<SysBizCarousel> sysBizCarousels = sysBizCarouselMapper.selectList(queryWrapper);
        return sysBizCarousels.size() < 6;
    }

    @Override
    public boolean selectGroundingNum2(SysBizCarousel sysBizCarousel) {
        LambdaQueryWrapper<SysBizCarousel> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SysBizCarousel::getDisplayed, UpDownShelvesEnum.GROUNDING.getKey());
        queryWrapper.eq(SysBizCarousel::getDeleted, OrderDeleteEnum.NOT_DELETE.getKey());
        queryWrapper.eq(SysBizCarousel::getTenantId, sysBizCarousel.getTenantId());
        queryWrapper.eq(SysBizCarousel::getType, sysBizCarousel.getType());
        queryWrapper.ne(sysBizCarousel.getId() != null, SysBizCarousel::getId, sysBizCarousel.getId());
        List<SysBizCarousel> sysBizCarousels = sysBizCarouselMapper.selectList(queryWrapper);
        return sysBizCarousels.size() < 4;
    }
}
